//负责人:李明洋
package util;

import java.util.regex.Pattern;
import java.util.Scanner;

public class Util {
    private static final Pattern ACCOUNT_PATTERN = Pattern.compile("^[a-zA-Z0-9]{3,20}$");
    private static final Pattern PASSWORD_PATTERN = Pattern.compile("^[a-zA-Z0-9]{6,20}$");
    private static final Pattern CODE_PATTERN = Pattern.compile("^[a-zA-Z0-9]{4}$");

    // 输入账号(利用正则表达式规定账号的约束条件)
    public static String loginAccount() {
        Scanner scanner = new Scanner(System.in);
        String account;
        do {
            System.out.print("请输入账号（3-20位字母或数字）: ");
            account = scanner.nextLine();
        } while (!ACCOUNT_PATTERN.matcher(account).matches());
        return account;
    }

    // 输入密码(利用正则表达式规定密码的约束条件)
    public static String loginPassword() {
        Scanner scanner = new Scanner(System.in);
        String password;
        do {
            System.out.print("请输入密码（6-20位字母或数字）: ");
            password = scanner.nextLine();
        } while (!PASSWORD_PATTERN.matcher(password).matches());
        return password;
    }

    // 把账号和密码添加到用户库里面
    public static void add(String UserAccount, String UserPassword) {
        // 这里可以实现添加到数据库或文件的逻辑
        System.out.println("账号 " + UserAccount + " 密码 " + UserPassword + " 已添加");
    }

    // 生成验证码(利用正则表达式规定验证码的约束条件)
    public static String verificationCode() {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = (int) (Math.random() * chars.length());
            code.append(chars.charAt(index));
        }
        return code.toString();
    }

    // 用户输入验证码
    public static String inputCode() {
        Scanner scanner = new Scanner(System.in);
        String code;
        do {
            System.out.print("请输入验证码（4位字母或数字）: ");
            code = scanner.nextLine();
        } while (!CODE_PATTERN.matcher(code).matches());
        return code;
    }

    // 检查验证码是否匹配
    public static boolean matchCode(String Code1, String Code2) {
        return Code1.equals(Code2);
    }
}
